package core.cards.election.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import core.cards.election.model.ElectionBackend;
import core.cards.election.model.ElectionResultBackend;
import core.utils.DialogPositioner;

public class DlgElectionResult extends Dialog {

	protected Object result;
	protected Shell shell;
	
	private ElectionBackend electionVotingCard;
	private ElectionResultBackend electionResult;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public DlgElectionResult(Shell parent, ElectionBackend electionBacked, ElectionResultBackend electionResultBackend) {
		super(parent);
		
		this.electionVotingCard = electionBacked;
		this.electionResult = electionResultBackend;
		
		setText("Election preview");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		DialogPositioner.centerShell(shell);
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
		shell.setSize(800, 600);
		shell.setText(getText());
		shell.setLayout(new GridLayout(1, false));
		
		Composite mainCmp = new Composite(shell, SWT.NONE);
		mainCmp.setLayout(new GridLayout(1, false));
		mainCmp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		Composite composite = new ElectionResultFrontend(mainCmp, electionResult, electionVotingCard);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
	}
}
